<!DOCTYPE html>
<html class="writer-html5" lang="en" data-content_root="../">
<head>
  <meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" />

  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Messages &mdash; The Logtalk Handbook v3.93.0-b01 documentation</title>
      <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=d75fae25" />
      <link rel="stylesheet" type="text/css" href="../_static/css/theme.css?v=19f00094" />
      <link rel="stylesheet" type="text/css" href="../_static/css/custom.css?v=396eccfe" />

  
  <!--[if lt IE 9]>
    <script src="../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
        <script src="../_static/jquery.js?v=5d32c60e"></script>
        <script src="../_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
        <script src="../_static/documentation_options.js?v=c8100655"></script>
        <script src="../_static/doctools.js?v=9a2dae69"></script>
        <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
    <script src="../_static/js/theme.js"></script>
    <!-- begin favicon -->
    <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
    <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png" />
    <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png" />
    <link rel="manifest" href="/site.webmanifest" />
    <link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5" />
    <meta name="msapplication-TileColor" content="#355b95" />
    <meta name="theme-color" content="#ffffff" />
    <!-- end favicon -->
    
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Objects" href="objects.html" />
    <link rel="prev" title="Nomenclature" href="nomenclature.html" />
   
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >

          
          
          <a href="../index.html" class="icon icon-home">
            The Logtalk Handbook
              <img src="../_static/logtalk.gif" class="logo" alt="Logo"/>
          </a>
              <div class="version">
                3.93.0
              </div>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
    
              <p class="caption" role="heading"><span class="caption-text">Contents</span></p>
<ul class="current">
<li class="toctree-l1 current"><a class="reference internal" href="index.html">User Manual</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="declarative.html">Declarative object-oriented programming</a></li>
<li class="toctree-l2"><a class="reference internal" href="features.html">Main features</a></li>
<li class="toctree-l2"><a class="reference internal" href="nomenclature.html">Nomenclature</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Messages</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#operators-used-in-message-sending">Operators used in message-sending</a></li>
<li class="toctree-l3"><a class="reference internal" href="#sending-a-message-to-an-object">Sending a message to an object</a></li>
<li class="toctree-l3"><a class="reference internal" href="#delegating-a-message-to-an-object">Delegating a message to an object</a></li>
<li class="toctree-l3"><a class="reference internal" href="#sending-a-message-to-self">Sending a message to <em>self</em></a></li>
<li class="toctree-l3"><a class="reference internal" href="#broadcasting">Broadcasting</a></li>
<li class="toctree-l3"><a class="reference internal" href="#calling-imported-and-inherited-predicates">Calling imported and inherited predicates</a></li>
<li class="toctree-l3"><a class="reference internal" href="#message-sending-and-event-generation">Message sending and event generation</a></li>
<li class="toctree-l3"><a class="reference internal" href="#sending-a-message-from-a-module">Sending a message from a module</a></li>
<li class="toctree-l3"><a class="reference internal" href="#message-sending-performance">Message sending performance</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="objects.html">Objects</a></li>
<li class="toctree-l2"><a class="reference internal" href="protocols.html">Protocols</a></li>
<li class="toctree-l2"><a class="reference internal" href="categories.html">Categories</a></li>
<li class="toctree-l2"><a class="reference internal" href="predicates.html">Predicates</a></li>
<li class="toctree-l2"><a class="reference internal" href="inheritance.html">Inheritance</a></li>
<li class="toctree-l2"><a class="reference internal" href="events.html">Event-driven programming</a></li>
<li class="toctree-l2"><a class="reference internal" href="threads.html">Multi-threading programming</a></li>
<li class="toctree-l2"><a class="reference internal" href="errors.html">Error handling</a></li>
<li class="toctree-l2"><a class="reference internal" href="reflection.html">Reflection</a></li>
<li class="toctree-l2"><a class="reference internal" href="programming.html">Writing and running applications</a></li>
<li class="toctree-l2"><a class="reference internal" href="printing.html">Printing messages and asking questions</a></li>
<li class="toctree-l2"><a class="reference internal" href="expansion.html">Term and goal expansion</a></li>
<li class="toctree-l2"><a class="reference internal" href="documenting.html">Documenting</a></li>
<li class="toctree-l2"><a class="reference internal" href="debugging.html">Debugging</a></li>
<li class="toctree-l2"><a class="reference internal" href="performance.html">Performance</a></li>
<li class="toctree-l2"><a class="reference internal" href="installing.html">Installing Logtalk</a></li>
<li class="toctree-l2"><a class="reference internal" href="migration.html">Prolog integration and migration</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../refman/index.html">Reference Manual</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tutorial/index.html">Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../faq/index.html">FAQ</a></li>
<li class="toctree-l1"><a class="reference internal" href="../devtools/index.html">Developer Tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="../libraries/index.html">Libraries</a></li>
<li class="toctree-l1"><a class="reference internal" href="../ports/index.html">Ports</a></li>
<li class="toctree-l1"><a class="reference internal" href="../contributions/index.html">Contributions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../glossary.html">Glossary</a></li>
<li class="toctree-l1"><a class="reference internal" href="../bibliography.html">Bibliography</a></li>
<li class="toctree-l1"><a class="reference internal" href="../genindex.html">Index</a></li>
</ul>

    <p class="caption"><span class="caption-text">External Contents</span></p>
    <ul>
    <li class="toctree-l1"><a class="reference internal" href="../../apis/index.html">APIs</a></li>
    <li class="toctree-l1"><a class="reference internal" href="https://logtalk.org">Logtalk website</a></li>
    <li class="toctree-l1"><a class="reference internal" href="https://github.com/LogtalkDotOrg/logtalk3">GitHub repo</a></li>
    </ul>
  
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">The Logtalk Handbook</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../index.html" class="icon icon-home" aria-label="Home"></a></li>
          <li class="breadcrumb-item"><a href="index.html">User Manual</a></li>
      <li class="breadcrumb-item active">Messages</li>
      <li class="wy-breadcrumbs-aside">
              <a href="https://github.com/LogtalkDotOrg/logtalk3/blob/master/docs/handbook/sources/userman/messages.rst" class="fa fa-github"> Edit on GitHub</a>
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <section id="messages">
<span id="messages-messages"></span><h1>Messages<a class="headerlink" href="#messages" title="Link to this heading"></a></h1>
<p>Messages allow us to ask an object to prove a goal and must always match a
declared predicate within the scope of the <em>sender</em> object. Note that sending
a message is fundamentally different from calling a predicate. When calling a
predicate, the caller decides implicitly which predicate definition will be
executed. When sending a message, it is the receiving object, not the sender,
that decides which predicate definition (if any) will be called to answer the
message. The predicate definition that is used to answer a message depends on
the relations between the object and its imported categories and ancestor
objects (if any). See the <a class="reference internal" href="inheritance.html#inheritance-inheritance"><span class="std std-ref">Inheritance</span></a> section for details
on the predicate declaration and predicate definition lookup procedures.</p>
<p>When a message corresponds to a <a class="reference internal" href="../glossary.html#term-meta-predicate"><span class="xref std std-term">meta-predicate</span></a>, the meta-arguments
are always called in the context of the object (or category) sending the
message.</p>
<p>Logtalk uses nomenclature similar to other object-oriented programming
languages such as Smalltalk. Therefore, the terms <em>query</em> and <em>message</em> are
used interchangeably when referring to a declared predicate that is part of
an object interface. Likewise, the terms <em>predicate</em> and <em>method</em> are used
interchangeably when referring to the predicate definition (inside an object
or category) that is called to answer a message.</p>
<section id="operators-used-in-message-sending">
<span id="messages-operators"></span><h2>Operators used in message-sending<a class="headerlink" href="#operators-used-in-message-sending" title="Link to this heading"></a></h2>
<p>Logtalk declares the following operators for the message-sending control
constructs:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="p">:- </span><span class="k">op</span>(<span class="m">600</span>, xfy, <span class="o">::</span>).
<span class="p">:- </span><span class="k">op</span>(<span class="m">600</span>,  fy, <span class="o">::</span>).
<span class="p">:- </span><span class="k">op</span>(<span class="m">600</span>,  fy, <span class="o">^^</span>).
</pre></div>
</div>
<p>It is assumed that these operators remain active (once the Logtalk
compiler and runtime files are loaded) until the end of the Prolog
session (this is the usual behavior of most Prolog compilers). Note that
these operator definitions are compatible with the predefined operators
in the Prolog ISO standard.</p>
</section>
<section id="sending-a-message-to-an-object">
<span id="messages-sending"></span><h2>Sending a message to an object<a class="headerlink" href="#sending-a-message-to-an-object" title="Link to this heading"></a></h2>
<p>Sending a message to an object is accomplished by using the
<a class="reference internal" href="../refman/control/send_to_object_2.html#control-send-to-object-2"><span class="std std-ref">(::)/2</span></a> control construct:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span>..., <span class="nv">Object</span><span class="o">::</span><span class="nv">Message</span>, ...
</pre></div>
</div>
<p>The message must match a public predicate declared for the receiving
object. The message may also correspond to a protected or private
predicate if the <em>sender</em> matches the predicate scope container. If the
predicate is declared but not defined, the message simply fails (as per
the <a class="reference internal" href="../glossary.html#term-closed-world-assumption"><span class="xref std std-term">closed-world assumption</span></a>).</p>
</section>
<section id="delegating-a-message-to-an-object">
<span id="messages-delegating"></span><h2>Delegating a message to an object<a class="headerlink" href="#delegating-a-message-to-an-object" title="Link to this heading"></a></h2>
<p>It is also possible to send a message to an object while preserving the
original <em>sender</em> and <em>meta-call context</em> by using the
<a class="reference internal" href="../refman/control/delegate_message_1.html#control-delegate-message-1"><span class="std std-ref">[]/1</span></a> delegation control construct:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span>..., [<span class="nv">Object</span><span class="o">::</span><span class="nv">Message</span>], ....
</pre></div>
</div>
<p>This control construct can only be used within objects and categories
(at the top-level interpreter, the <em>sender</em> is always the pseudo-object
<code class="docutils literal notranslate"><span class="pre">user</span></code> so using this control construct would be equivalent to using the
<code class="docutils literal notranslate"><span class="pre">(::)/2</span></code> message-sending control construct).</p>
</section>
<section id="sending-a-message-to-self">
<h2>Sending a message to <em>self</em><a class="headerlink" href="#sending-a-message-to-self" title="Link to this heading"></a></h2>
<p>While defining a predicate, we sometimes need to send a message to
<em>self</em>, i.e., to the same object that has received the original message.
This is done in Logtalk through the
<a class="reference internal" href="../refman/control/send_to_self_1.html#control-send-to-self-1"><span class="std std-ref">(::)/1</span></a> control construct:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span>..., <span class="o">::</span><span class="nv">Message</span>, ....
</pre></div>
</div>
<p>The message must match either a public or protected predicate declared for
the receiving object or a private predicate within the scope of the <em>sender</em>
otherwise an error will be thrown. If the message is sent from inside a
category or if we are using private inheritance, then the message may also
match a private predicate. Again, if the predicate is declared but not
defined, the message simply fails (as per the <a class="reference internal" href="../glossary.html#term-closed-world-assumption"><span class="xref std std-term">closed-world assumption</span></a>).</p>
</section>
<section id="broadcasting">
<span id="messages-broadcasting"></span><h2>Broadcasting<a class="headerlink" href="#broadcasting" title="Link to this heading"></a></h2>
<p>In the Logtalk context, <em>broadcasting</em> is interpreted as the sending of
several messages to the same object. This can be achieved by using the
message-sending control construct described above. However, for convenience,
Logtalk implements an extended syntax for message-sending that may improve
program readability in some cases. This extended syntax uses the <code class="docutils literal notranslate"><span class="pre">(,)/2</span></code>,
<code class="docutils literal notranslate"><span class="pre">(;)/2</span></code>, and <code class="docutils literal notranslate"><span class="pre">(-&gt;)/2</span></code> control constructs (plus the <code class="docutils literal notranslate"><span class="pre">(*-&gt;)/2</span></code> soft-cut
control construct when provided by the backend Prolog compiler). For example,
if we wish to send several messages to the same object, we can write:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>| ?- Object::(Message1, Message2, ...).
</pre></div>
</div>
<p>This is semantically equivalent to:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>| ?- Object::Message1, Object::Message2, ... .
</pre></div>
</div>
<p>This extended syntax may also be used with the <code class="docutils literal notranslate"><span class="pre">(::)/1</span></code> message-sending
control construct.</p>
</section>
<section id="calling-imported-and-inherited-predicates">
<span id="messages-super"></span><h2>Calling imported and inherited predicates<a class="headerlink" href="#calling-imported-and-inherited-predicates" title="Link to this heading"></a></h2>
<p>When redefining a predicate, sometimes we need to call the inherited
definition in the new code. This functionality, introduced by the
Smalltalk language through the <code class="docutils literal notranslate"><span class="pre">super</span></code> primitive, is available in
Logtalk using the <a class="reference internal" href="../refman/control/call_super_1.html#control-call-super-1"><span class="std std-ref">(^^)/1</span></a> control construct:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span>..., <span class="o">^^</span><span class="nv">Predicate</span>, ....
</pre></div>
</div>
<p>Most of the time we will use this control construct by instantiating the
pattern:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="nv">Predicate</span> <span class="o">:-</span>
    ...,            <span class="c">% do something</span>
    <span class="o">^^</span><span class="nv">Predicate</span>,    <span class="c">% call inherited definition</span>
    ... .           <span class="c">% do something more</span>
</pre></div>
</div>
<p>This control construct is generalized in Logtalk where it may be used to
call any imported or inherited predicate definition. This control
construct may be used within objects and categories. When combined with
<a class="reference internal" href="../glossary.html#term-static-binding"><span class="xref std std-term">static binding</span></a>, this control construct allows imported and inherited
predicates to be called with the same performance as local predicates.
As with the message-sending control constructs, the <code class="docutils literal notranslate"><span class="pre">(^^)/1</span></code> call simply
fails when the predicate is declared but not defined (as per the
<a class="reference internal" href="../glossary.html#term-closed-world-assumption"><span class="xref std std-term">closed-world assumption</span></a>).</p>
</section>
<section id="message-sending-and-event-generation">
<span id="messages-events"></span><h2>Message sending and event generation<a class="headerlink" href="#message-sending-and-event-generation" title="Link to this heading"></a></h2>
<p>Assuming the <a class="reference internal" href="programming.html#flag-events"><span class="std std-ref">events</span></a> flag is set to <code class="docutils literal notranslate"><span class="pre">allow</span></code> for the
object (or category) sending a message using the
<a class="reference internal" href="../refman/control/send_to_object_2.html#control-send-to-object-2"><span class="std std-ref">(::)/2</span></a> control construct, two events are generated,
one before and one after the message execution.
Messages that are sent using the
<a class="reference internal" href="../refman/control/send_to_self_1.html#control-send-to-self-1"><span class="std std-ref">(::)/1</span></a> (message to <em>self</em>)
control construct or the
<a class="reference internal" href="../refman/control/call_super_1.html#control-call-super-1"><span class="std std-ref">(^^)/1</span></a> super mechanism
described above do not generate any events. The rationale behind this
distinction is that messages to <em>self</em> and <em>super</em> calls are only used
internally in the definition of methods or to execute additional
messages with the same target object (represented by <em>self</em>). In other
words, events are only generated when using an object’s public
interface; they cannot be used to break object encapsulation.</p>
<p>If we need to generate events for a public message sent to <em>self</em>, then
we just need to write something like:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="nv">Predicate</span> <span class="o">:-</span>
    ...,
    <span class="c">% get self reference</span>
    <span class="k">self</span>(<span class="nv">Self</span>),
    <span class="c">% send a message to self using (::)/2</span>
    <span class="nv">Self</span><span class="o">::</span><span class="nv">Message</span>,
    ... .
</pre></div>
</div>
<p>If we also need the sender of the message to be other than the object
containing the predicate definition, we can write:</p>
<div class="highlight-logtalk notranslate"><div class="highlight"><pre><span></span><span class="nv">Predicate</span> <span class="o">:-</span>
    ...,
    <span class="c">% send a message to self using (::)/2</span>
    <span class="c">% sender will be the pseudo-object user</span>
    <span class="k">self</span>(<span class="nv">Self</span>),
    <span class="k">{</span><span class="nv">Self</span><span class="o">::</span><span class="nv">Message</span><span class="k">}</span>,
    ... .
</pre></div>
</div>
<p>When events are not used, it is possible to turn off event generation globally
or on a per-entity basis by using the <code class="docutils literal notranslate"><span class="pre">events</span></code> compiler flag to optimize
message-sending performance (see the <a class="reference internal" href="events.html#events-events"><span class="std std-ref">Event-driven programming</span></a> section for more
details).</p>
</section>
<section id="sending-a-message-from-a-module">
<span id="messages-from-module"></span><h2>Sending a message from a module<a class="headerlink" href="#sending-a-message-from-a-module" title="Link to this heading"></a></h2>
<p>Messages can be sent to objects from within Prolog modules. Depending on the
backend support for goal-expansion and on the <a class="reference internal" href="programming.html#flag-optimize"><span class="std std-ref">optimize</span></a>
flag being turned on, the messages will use static binding when possible. This
optimization requires the object to be compiled and loaded before the module.
Note that the module can be <code class="docutils literal notranslate"><span class="pre">user</span></code>. This is usually the case when sending
the message from the top-level interpreter. Thus, the same conditions apply
in this case. Note that loading Prolog modules using Prolog directives or
built-in predicates necessarily limits the range of possible optimizations
for messages sent from the modules.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>If you want to benchmark the performance of a message-sending goal
at the top-level interpreter, be careful to check first if the goal
is pre-compiled to use static binding; otherwise you will also be
benchmarking the Logtalk compiler itself.</p>
</div>
</section>
<section id="message-sending-performance">
<span id="messages-performance"></span><h2>Message sending performance<a class="headerlink" href="#message-sending-performance" title="Link to this heading"></a></h2>
<p>For a detailed discussion on message-sending performance, see the
<a class="reference internal" href="performance.html#performance-performance"><span class="std std-ref">Performance</span></a> section.</p>
</section>
</section>


           </div>
          </div>
          <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
        <a href="nomenclature.html" class="btn btn-neutral float-left" title="Nomenclature" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
        <a href="objects.html" class="btn btn-neutral float-right" title="Objects" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 1998-2025, Paulo Moura.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>